import { defineStore } from 'pinia'
import { store } from '@/stores'

export const useLivePusherStore = defineStore({
  id: 'live-broadcast-room',
  state: () => ({
		liveBroadcastList: [],
		openLiveBroadcastList: [],
  }),
  actions: {
		SET_LIVE_BROADCAST(liveBroadcastList) {
			this.liveBroadcastList = liveBroadcastList
		},
		SET_OPEN_LIVE_BROADCAST_LIST(openLiveBroadcastList) {
			this.openLiveBroadcastList = openLiveBroadcastList
		},
		SET_NEW_LIVE_BROADCAST(liveBroadcast) {
			const isLiveBroadcastList = this.openLiveBroadcastList.some(item => {
				return Object.values(item).includes(liveBroadcast.live_broadcast_room)
			})
			if(isLiveBroadcastList) { return }
			this.openLiveBroadcastList.push(liveBroadcast)
		},
		STOP_LIVE_BROADCAST(room) {
			const delRoom = this.openLiveBroadcastList.filter(item => item.live_broadcast_room !== room);
			this.openLiveBroadcastList = delRoom
		}
  }
})

export function useLivePusherStoreHook() {
  return useLivePusherStore(store)
}
